<?php
namespace app\index\controller;

use think\Db;
use think\Request;
use app\admin\model\Category as CategoryModel;
use app\admin\model\DataTable as DataTableModel;
use app\admin\model\TableExtraColumn as TableExtraColumnModel;

class DataContent extends Base
{
    public function index()
    {
        echo json_encode($this->categories);
        // echo "";
    }

    public function edataClass($cid = 0)
    {
        $data = $this->category_model->find($cid);
        
        $data->child = $this->category_model->where('pid', $cid)->select();

        $data->parent = $this->category_model->where('id', $data->pid)->find();
        // echo json_encode($data);
        // exit;

        $this->assign('data', $data);
        $this->assign('cid', $cid);
        return $this->fetch();
    }

    public function dataList(Request $request = null, $cid = 0, $tablename = '', $q = '')
    {   
        $data_category = $this->category_model->find($cid);

        $data_category->parent = $this->category_model->where('id', $data_category->pid)->find();
        
        $data_category->grandparent = $this->category_model->where('id', $data_category->parent->pid)->find();
        // echo json_encode($data_category);
        // exit;

        if ($tablename) {
            $pageSize = 15;
            if ($q) {
                $query['q'] = $q;

                $data = Db::table($tablename)->where('title', 'LIKE', '%'.$q.'%')->field('id, title, username, clicks, create_time')->order('id', 'desc')->paginate($pageSize, false, ['query'=>$query]);
            } else {
                $data = Db::table($tablename)->field('id, title, username, clicks, create_time')->order('id', 'desc')->paginate($pageSize);
            }
            $this->assign('data', $data);
        }
        
        $this->assign('data_category', $data_category);
        $this->assign('tablename', $tablename);
        $this->assign('cid', $cid);
        $this->assign('q', $q);
        return $this->fetch();
    }

    public function detail($cid = 0, $tablename = '', $id=0)
    {
        $data_category = $this->category_model->find($cid);

        $data_category->parent = $this->category_model->where('id', $data_category->pid)->find();
        
        $data_category->grandparent = $this->category_model->where('id', $data_category->parent->pid)->find();
        
        if ($tablename) {
            $table_id = DataTableModel::where('table_name_en', $tablename)->value('id');
            $table_extra_columns = TableExtraColumnModel::where('table_id', $table_id)->field('column_name_en,column_name_cn,note')->select();
            $data = Db::table($tablename)->find($id);
            $this->assign('data', $data);
            $this->assign('table_extra_columns', $table_extra_columns);

             // 点击量加 1
            Db::table($tablename)->where('id', $data['id'])->update(['clicks'=>$data['clicks'] +1]);
            $this->category_model->save(['onclick'=>$data_category['onclick']+1], ['id'=>$cid]);
        }
        
        $this->assign('cid', $cid);
        $this->assign('tablename', $tablename);
        $this->assign('data_category', $data_category);
        // 图谱数据
        if ($tablename=='t_metallographic_phase_image' || $tablename=='t_nature_metallograph_image' || $tablename=='t_industry_image' ) {
            return $this->fetch('photo');    
        }
        return $this->fetch();
    }
}
